﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MailSender.Utils
{
	class FileInfoEx
	{
		private FileInfo fi;
		private string exponent = string.Empty;
		private long size = -1;

		public string FullFileName { get; set; }

		public long Size { get { return fi.Length; } }

		public override string ToString()
		{
			return string.Format("{0} ({1}{2})", fi.Name, size, exponent);
		}

		public FileInfoEx(string fullFileName)
		{
			this.FullFileName = fullFileName;
			if (!File.Exists(fullFileName))
				throw new IOException(string.Format("File does not exists: {0}", fullFileName));

			fi = new FileInfo(this.FullFileName);
			CountSize();
		}


		private void CountSize()
		{
			int exp = 0;
			this.size = fi.Length;
			while (size > 1024)
			{
				exp++;
				size /= 1024;
			}

			exponent = string.Empty;
			switch (exp)
			{
				case 0: exponent = "B"; break;
				case 1: exponent = "kB"; break;
				case 2: exponent = "MB"; break;
				case 3: exponent = "GB"; break;
			}
		}

		public static string GetReadableFileSize(long size)
		{
			int exp = 0;
			long s = size;
			while (s > 1024)
			{
				exp++;
				s /= 1024;
			}

			string exponent = string.Empty;
			switch (exp)
			{
				case 0: exponent = "B"; break;
				case 1: exponent = "kB"; break;
				case 2: exponent = "MB"; break;
				case 3: exponent = "GB"; break;
			}

			return string.Format("{0}{1}", s, exponent);
		}
	}
}
